/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alipay.client.util.StringUtil;
import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.wechat.service.WechatDaoHelper;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.BaseEntity;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 抽奖Controller
 * @author Jakemanse
 * @version 2013-09-23
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/statics")
public class StaticsController extends BaseController {

	
	@Autowired
	private WechatDaoHelper wechatDaoHelper;
	
	
	
	@RequestMapping(value = "members")
	public String queryMembers(HttpServletRequest request, HttpServletResponse response, Model model,RedirectAttributes redirectAttributes){
		Map<String,Object> params = new HashMap<String, Object>();
		User user = UserUtils.getUser();
		
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		if(StringUtil.isEmpty(startTime)){
			//startTime = DateUtils.formatDate(new Date(), "yyyy-MM-dd 00:00:00");
		}
		
		params.put("startTime", startTime);
		params.put("endTime", request.getParameter("endTime"));
		params.put("userId", user.getId());
		 
		List<Map<String, Object>> statics = wechatDaoHelper.queryMembers(params);
		model.addAttribute("statics", statics);
		model.addAttribute("user", user);
		return "modules/wechat/statics/memberStatics";
	}
	
	
	

	/**
	 * @author Jakemanse
	 * @time 2013-10-10  上午10:18:31
	 * @function <p>  </p>
	 * @param params
	 * @param request
	 * @param response
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "st")
	public String statics  (HttpServletRequest request, HttpServletResponse response, Model model,RedirectAttributes redirectAttributes) {
		
		Map<String,String> params  = new HashMap<String,String>();
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		if(StringUtil.isEmpty(startTime)){
			startTime = DateUtils.formatDate(new Date(), "yyyy-MM-dd 00:00:00");
		}
		
		params.put("startTime",startTime);
		params.put("endTime", endTime);
		
		params.put("day", request.getParameter("day"));
		params.put("month", request.getParameter("month"));
		params.put("awardType", request.getParameter("awardType"));
		params.put("status", request.getParameter("status"));
		params.put("userId", request.getParameter("userId"));
		params.put("queryType", request.getParameter("queryType"));	// day, month
		
		if(params!=null){
			if(params.get("day") == null && params.get("month") ==  null){
				params.put("day", com.boxin.framework.base.utils.DateUtils.getTodayString());
			}
		}
		
		List statics = null;
		if("month".equals(request.getParameter("queryType"))){	//按月
			if( params.get("month") ==  null){
				params.put("month", com.boxin.framework.base.utils.DateUtils.getYearMonthString());
			}
			statics =  wechatDaoHelper.queryAwardStaticsByMonth(params);
		}else if("day".equals(request.getParameter("queryType"))){	//按日
			statics =  wechatDaoHelper.queryAwardStaticsByDay(params);
		}else{
			statics =  wechatDaoHelper.queryAwardStaticsByMonth(params);//默认按日
		}
		
		model.addAttribute("statics", statics);
		model.addAttribute("params", params);
		model.addAttribute("queryType", request.getParameter("queryType"));
		model.addAttribute("awardType",request.getParameter("awardType"));
		model.addAttribute("startTime", startTime);
		model.addAttribute("endTime", endTime);
		
		return "modules/wechat/statics/awardStatics";
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-10-14  上午12:47:40
	 * @function <p>  </p>
	 * @param request
	 * @param response
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "stat")
	public String queryLotteryTotalData  (HttpServletRequest request, HttpServletResponse response, Model model,RedirectAttributes redirectAttributes) {
		
		Map<String,String> params  = new HashMap<String,String>();
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		if(StringUtil.isEmpty(startTime)){
			startTime = DateUtils.formatDate(new Date(), "yyyy-MM-dd 00:00:00");
		}
		
		
		User user = UserUtils.getUser();
		params.put("startTime",startTime);
		params.put("endTime", endTime);
		params.put("userId", user.getId()+"");
		params.put("status", BaseEntity.STATUS_NORMAL);
		
		List statics =   wechatDaoHelper.queryAllStatusLotteries(params);// 已核销
		
		params.put("status", BaseEntity.STATUS_CLEAR);
		List statics2 =  wechatDaoHelper.queryAllStatusLotteries(params);// 未使用的
		
		List result = new ArrayList();
		result.addAll(statics);
		result.addAll(statics2);
		
		model.addAttribute("statics", result);
		model.addAttribute("user", user);
		model.addAttribute("startTime", startTime);
		model.addAttribute("endTime", endTime);
		
		return "modules/wechat/statics/lotteryDataStatics";
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-10-15  下午4:25:20
	 * @function <p>  </p>
	 * @param request
	 * @param response
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "statCategory")
	public String queryLotteryCategoryData  (HttpServletRequest request, HttpServletResponse response, Model model,RedirectAttributes redirectAttributes) {
		
		Map<String,String> params  = new HashMap<String,String>();
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		if(StringUtil.isEmpty(startTime)){
			startTime = DateUtils.formatDate(new Date(), "yyyy-MM-dd 00:00:00");
		}
		
		User user = UserUtils.getUser();
		params.put("startTime", startTime);
		params.put("endTime", endTime);
		params.put("userId", user.getId()+"");
		params.put("status", BaseEntity.STATUS_NORMAL);
		
		List statics =  wechatDaoHelper.queryCategoryLotteries(params);
		
		model.addAttribute("statics", statics);
		model.addAttribute("user", user);
		model.addAttribute("startTime", startTime);
		model.addAttribute("endTime", endTime);
		
		return "modules/wechat/statics/lotteryCategoryDataStatics";
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-10-15  下午4:25:44
	 * @function <p> 统计核销 </p>
	 * @param request
	 * @param response
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "statUsed")
	public String queryAllUsedAwards(HttpServletRequest request, HttpServletResponse response, Model model,RedirectAttributes redirectAttributes) {
		
		Map<String,String> params  = new HashMap<String,String>();
		User user = UserUtils.getUser();
		
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		if(StringUtil.isEmpty(startTime)){
			startTime = DateUtils.formatDate(new Date(), "yyyy-MM-dd 00:00:00");
		}
		
		params.put("startTime", startTime);
		params.put("endTime", endTime);
		params.put("userId", user.getId()+"");
		
		List statics =  wechatDaoHelper.queryAllUsedAwards(params);
		List statics2 =  wechatDaoHelper.queryAllUnusedAwards(params);
		
//		for(Object map : statics){
//			Map<String,Object> kv = (Map<String,Object>)map;
//			for(Object map2 : statics2){
//				Map<String,Object> kv2 = (Map<String,Object>)map2;
//				if(kv.get("NAME")!=null){
//					
//					if(kv.get("NAME").equals(kv2.get("NAME"))){
//						kv.put("unused_cnt", kv2.get("cnt"));
//					}
//				}
//			}
//		}
		List result = new ArrayList();
		result.addAll(statics);
//		result.addAll(statics2);
		
		model.addAttribute("statics", statics);
		model.addAttribute("user", user);
		model.addAttribute("startTime", startTime);
		model.addAttribute("endTime", endTime);
		
		return "modules/wechat/statics/allUsedDataStatics";
	}
	 
	
	/**
	 * @author Jakemanse
	 * @time 2013-11-14  上午12:18:13
	 * @function <p> 按终端统计 </p>
	 * @param request
	 * @param response
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "statTerminalUsed")
	public String queryAllUsedAwardsByTerminal(HttpServletRequest request, HttpServletResponse response, Model model,RedirectAttributes redirectAttributes) {
		
		Map<String,String> params  = new HashMap<String,String>();
		User user = UserUtils.getUser();
		
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		if(StringUtil.isEmpty(startTime)){
			startTime = DateUtils.formatDate(new Date(), "yyyy-MM-dd 00:00:00");
		}
		
		params.put("startTime", startTime);
		params.put("endTime", endTime);
		params.put("userId", user.getId()+"");
		
		List statics =  wechatDaoHelper.queryAllUsedAwardsByTerminal(params);
		
//		for(Object map : statics){
//			Map<String,Object> kv = (Map<String,Object>)map;
//			for(Object map2 : statics2){
//				Map<String,Object> kv2 = (Map<String,Object>)map2;
//				if(kv.get("NAME")!=null){
//					
//					if(kv.get("NAME").equals(kv2.get("NAME"))){
//						kv.put("unused_cnt", kv2.get("cnt"));
//					}
//				}
//			}
//		}
		List result = new ArrayList();
		result.addAll(statics);
//		result.addAll(statics2);
		
		model.addAttribute("statics", statics);
		model.addAttribute("user", user);
		model.addAttribute("startTime", startTime);
		model.addAttribute("endTime", endTime);
		
		return "modules/wechat/statics/terminalUsedDataStatics";
	}
	
	@RequestMapping(value = "detail")
	public String queryDetailAwards(Map<String,String> search,HttpServletRequest request, HttpServletResponse response, Model model,RedirectAttributes redirectAttributes) {
		
		Map<String,String> params  = new HashMap<String,String>();
		
		
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		if(StringUtil.isEmpty(startTime)){
			startTime = DateUtils.formatDate(new Date(), "yyyy-MM-dd 00:00:00");
		}
		String memberName = request.getParameter("memberName");
		String name = request.getParameter("name");
		String status =  request.getParameter("status");
		String awardType=request.getParameter("awardType");
		String mobile=request.getParameter("mobile");
		
		User user = UserUtils.getUser();
		params.put("startTime", startTime);
		params.put("endTime", endTime);
		params.put("userId", user.getId()+"");
		params.put("memberName", memberName);
		params.put("name", name );
		params.put("status", status);
		params.put("mobile", mobile);
		params.put("awardType", awardType);
		
		List statics =  wechatDaoHelper.queryMemberAwardsDetails(params);
		
		model.addAttribute("statics", statics);
		model.addAttribute("user", user);
		model.addAttribute("startTime", startTime);
		model.addAttribute("endTime", endTime);
		model.addAttribute("awardType", awardType);
		model.addAttribute("endTime", endTime);
		model.addAttribute("status", status);
		
		return "modules/wechat/statics/memberAwardDetails";
	}
	
	
	
	public Map<String,String> getStringTimeMap(int type){
		Map<String,String> timeMap = new HashMap<String,String>();
		switch(type){
			case 1:
				timeMap.put("startTime", DateUtils.formatDate(new Date(), "yyyy-MM-dd 00:00:00"));
				timeMap.put("endTime", DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
				
				break;
			case 2:
				
			break;
			default:
				break;
		}
		
		return timeMap;
	}
	
	 

}
